import os

from django.utils import simplejson
from google.appengine.ext import webapp
# from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util

print "Content-Type: application/json"
print ""

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write("poll.py")

class RPCHandler(webapp.RequestHandler):
  def __init__(self):
    webapp.RequestHandler.__init__(self)
    self.methods = RPCMethods()

  def get(self):
    func = None
    action = self.request.get('action')
    if action:
      if action[0] == '_':
        self.error(403)
        return
      else:
        func = getattr(self.methods, action, None)

    if not func:
      self.error(404)
      return

    args = ()
    while True:
      key = 'arg%d' % len(args)
      val = self.request.get(key)
      if val:
        args+= (simplejson.loads(val),)
      else:
        break;
    result = func(*args)
    self.response.out.write(simplejson.dumps(result))

class RPCMethods:
  def Add(self, *args):
    ints = [int(arg) for arg in args]
    return sum(ints)

def main():
  app = webapp.WSGIApplication([
    ('/gadgets/poll/', MainPage),
    ('/gadgets/poll/rpc', RPCHandler),
    ], debug=True)
  util.run_wsgi_app(app)

if __name__ == '__main__':
  main()
